package com.clerkq.Scroller
{
	import com.content.model.LoadedAssets.SkinProxy;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;

    public class DragBlock extends UIObject implements IDisposable {

        private var _pressed:Boolean;
        private var _balken:Sprite;
        private var _slider:Sprite;
        private var _sliderOver:Sprite;
        
        private var _direction:String = ScrollerType.VERTICAL;

        public static const ON_RELEASE_SLIDER:String = "onReleaseSlider";
        public static const ON_PRESS_SLIDER:String = "onPressSlider";

        public function DragBlock(direction:String){
			this.mouseChildren = false;
			
            _direction = direction;
			
			_slider = new SkinProxy.IMG_SSlider() as Sprite;
			_sliderOver = new SkinProxy.IMG_SSliderOver() as Sprite;
			_balken = new SkinProxy.IMG_Balken() as Sprite;
			
			if(_direction == ScrollerType.HORIZONTAL){
				_slider.y = _sliderOver.y = _slider.width;
				_balken.rotation = -90;
				_balken.y = int(_slider.width - (_slider.width - _balken.width)/2 );
			}else{
				_balken.x = int( (_slider.width - _balken.width)/2 )+1;
			}
			
			addChild(_slider);
			addChild(_sliderOver);
			addChild(_balken);
			_sliderOver.alpha = 0;
			
			this.addEventListener(Event.ADDED_TO_STAGE, OnAdded);
        }
        
        // Events
        public function BtnOver(evt:MouseEvent):void{
			TweenLite.to(_sliderOver, 0.4, {alpha: 1.0});
        }
        public function BtnOut(evt:MouseEvent):void{
            if (_pressed){    	return;         };            
			TweenLite.to(_sliderOver, 0.4, {alpha: 0});
        }
        public function BtnDown(evt:MouseEvent):void{
        	//evt.stopImmediatePropagation();
            _pressed = true;
            
            this.dispatchEvent(new Event(ON_PRESS_SLIDER, false, false));
        }
        public function BtnUp(evt:MouseEvent):void{
            _pressed = false;
            
			if(_sliderOver.hitTestPoint(evt.stageX, evt.stageY)){
			}else{
				TweenLite.to(_sliderOver, 0.4, {alpha: 0});
			}
			
            this.dispatchEvent(new Event(ON_RELEASE_SLIDER, false, false));
        }
		private function OnAdded(evt:Event):void{
			this.addEventListener(MouseEvent.MOUSE_OVER, BtnOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, BtnOut);
			this.addEventListener(MouseEvent.MOUSE_DOWN, BtnDown);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, BtnUp);
		}
        
        // Get and Set
        public function set sliderHeight(value:int):void{
            _slider.height = _sliderOver.height = value;
            _balken.y = int( (value - _balken.height)/2 );
        }
        public function set sliderWidth(value:int):void{
        	_slider.rotation = _sliderOver.rotation = 0;
            _slider.height = _sliderOver.height = value;
			_slider.rotation = _sliderOver.rotation = -90;
            
        	// 把形状横过来
            _balken.x = int( (value - _balken.height)/2 );
        }
		
        public function Dispose():void{
			//this.stage.removeEventListener(MouseEvent.MOUSE_UP, BtnUp);
			this.addEventListener(Event.ADDED_TO_STAGE, OnAdded);
			this.removeEventListener(MouseEvent.MOUSE_OVER, BtnOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT, BtnOut);
			this.removeEventListener(MouseEvent.MOUSE_DOWN, BtnDown);
        }
    }
}